package com.neusoft.customerserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.customerserver.dto.QueryCustomerRelationship;
import com.neusoft.customerserver.dto.RelatedCustomer;
import com.neusoft.customerserver.dto.RespBean;
import com.neusoft.customerserver.entity.Customer;
import com.neusoft.customerserver.entity.CustomerCustomer;
import com.neusoft.customerserver.mapper.CustomerCustomerMapper;
import com.neusoft.customerserver.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户关联表 前端控制器
 * </p>
 *
 * @author victor
 * @since 2021-07-11
 */
@RestController
@RequestMapping("/customer")
public class CustomerCustomerController {
    @Autowired
    private CustomerCustomerMapper cusCusMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @PostMapping("/addCustomerRelationship")
    public RespBean addCustomerRelationship(@RequestBody List<CustomerCustomer> cusCusList){
        int num = 0;
        for (CustomerCustomer cusCus : cusCusList){
            num += cusCusMapper.insert(cusCus);
        }
        if(num == cusCusList.size()){
            return RespBean.ok(200,"Add Success",cusCusList);
        }else {
            return RespBean.error(500,"ACCESS DENIED",cusCusList);
        }
    }

    @PostMapping("/updateCustomerRelationship")
    public RespBean updateCustomerRelationship(@RequestBody CustomerCustomer cusCus){
        QueryWrapper<CustomerCustomer> qw = Wrappers.query();
        qw.eq("relative_customer_id1",cusCus.getRelativeCustomerId1()).eq("relative_customer_id2",cusCus.getRelativeCustomerId2());
        int num = cusCusMapper.update(cusCus,qw);
        if(num == 1){
            return RespBean.ok(200,"Update Success",cusCus);
        }else {
            return RespBean.error(500,"ACCESS DENIED",cusCus);
        }
    }

    @PostMapping("/deleteCustomerRelationshipById")
    public RespBean deleteCustomerRelationship(@RequestBody CustomerCustomer cusCus){
        QueryWrapper<CustomerCustomer> qw = Wrappers.query();
        qw.eq("relative_customer_id1",cusCus.getRelativeCustomerId1()).eq("relative_customer_id2",cusCus.getRelativeCustomerId2());
        int num = cusCusMapper.delete(qw);
        if(num > 0){
            return RespBean.ok(200,"Delete Success",cusCus);
        }else {
            return RespBean.error(500,"ACCESS DENIED");
        }
    }

    @PostMapping("/queryAllCustomerRelationship")
    public RespBean queryAllCustomerRelationship(@RequestBody Customer customer){
        QueryWrapper<CustomerCustomer> qw = Wrappers.query();
        qw.eq("relative_customer_id1",customer.getCustomerId());
        List<CustomerCustomer> cusCusList = cusCusMapper.selectList(qw);
        List<RelatedCustomer> relCusList = new ArrayList<>();
        for (CustomerCustomer cusCus : cusCusList){
            RelatedCustomer rc = new RelatedCustomer(cusCus);
            Customer c = customerMapper.selectById(rc.getRelativeCustomerId2());
            rc.setRelativeCustomerName(c.getCustomerName());
            relCusList.add(rc);
        }
        if (cusCusList.size() < 1){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"All Relationship Search success",relCusList);
        }

    }
}

